package com.data.modules.online.config.executor;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.toolkit.JdbcUtils;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import com.data.common.util.CommonUtils;
import com.data.common.util.SpringContextUtils;
import com.data.common.util.dynamic.db.DbTypeUtils;
import com.data.common.util.ConvertUtils;
import com.data.modules.online.config.exception.DBException;
import com.data.modules.online.config.service.DbTableHandleI;
import com.data.modules.online.config.datasourceConfig.DataBaseConfig;
import com.data.modules.online.config.service.p020a.DbTableDB2HandleImpl;
import com.data.modules.online.config.service.p020a.DbTableDmHandleImpl;
import com.data.modules.online.config.service.p020a.DbTableHyperSQLHandleImpl;
import com.data.modules.online.config.service.p020a.DbTableMysqlHandleImpl;
import com.data.modules.online.config.service.p020a.DbTableOracleHandleImpl;
import com.data.modules.online.config.service.p020a.DbTablePostgresHandleImpl;
import com.data.modules.online.config.service.p020a.DbTableSQLServerHandleImpl;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

@Slf4j
@SuppressWarnings("ALL")
public class DbTableUtil {

    public static String f481a = "";

    private static final Logger f482b = LoggerFactory.getLogger(DbTableUtil.class);

    public static DbTableHandleI getTableHandle() throws SQLException, DBException {
        return m434a((DataBaseConfig) null);
    }

    public static DbTableHandleI m434a(DataBaseConfig bVar) throws SQLException, DBException {
        String dbTypeString = DbTypeUtils.getDbTypeString(getDbType(bVar));
        char c = 65535;
        switch (dbTypeString.hashCode()) {
            case -1955532418:
                if (dbTypeString.equals("ORACLE")) {
                    c = 2;
                    break;
                }
                break;
            case -1620389036:
                if (dbTypeString.equals("POSTGRESQL")) {
                    c = 5;
                    break;
                }
                break;
            case 2185:
                if (dbTypeString.equals("DM")) {
                    c = 3;
                    break;
                }
                break;
            case 67444:
                if (dbTypeString.equals("DB2")) {
                    c = 6;
                    break;
                }
                break;
            case 2227302:
                if (dbTypeString.equals("HSQL")) {
                    c = 7;
                    break;
                }
                break;
            case 73844866:
                if (dbTypeString.equals("MYSQL")) {
                    c = 0;
                    break;
                }
                break;
            case 912124529:
                if (dbTypeString.equals("SQLSERVER")) {
                    c = 4;
                    break;
                }
                break;
            case 1557169620:
                if (dbTypeString.equals("MARIADB")) {
                    c = 1;
                    break;
                }
                break;
        }
        switch (c) {
            case 0:
                return new DbTableMysqlHandleImpl();
            case 1:
                return new DbTableMysqlHandleImpl();
            case 2:
                return new DbTableOracleHandleImpl();
            case 3:
                return new DbTableDmHandleImpl();
            case 4:
                return new DbTableSQLServerHandleImpl();
            case 5:
                return new DbTablePostgresHandleImpl();
            case 6:
                return new DbTableDB2HandleImpl();
            case 7:
                return new DbTableHyperSQLHandleImpl();
            default:
                return new DbTableMysqlHandleImpl();
        }
    }

    public static Connection getConnection() throws SQLException {
        return ((DataSource) SpringContextUtils.getApplicationContext().getBean(DataSource.class)).getConnection();
    }

    public static String getDatabaseType() throws SQLException, DBException {
        if (ConvertUtils.isNotEmpty(f481a)) {
            return f481a;
        }
        return m432a((DataSource) SpringContextUtils.getApplicationContext().getBean(DataSource.class));
    }

    public static boolean m435a() {
        try {
            return "ORACLE".equals(getDatabaseType());
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (DBException e2) {
            e2.printStackTrace();
        }
        return false;
    }

    public static String m432a(DataSource dataSource) throws SQLException, DBException {
        if ("".equals(f481a)) {
            Connection connection = dataSource.getConnection();
            try {
                String lowerCase = connection.getMetaData().getDatabaseProductName().toLowerCase();
                if (lowerCase.indexOf("mysql") >= 0) {
                    f481a = "MYSQL";
                } else if (lowerCase.indexOf("oracle") >= 0) {
                    f481a = "ORACLE";
                } else if (lowerCase.indexOf("dm") >= 0) {
                    f481a = "DM";
                } else if (lowerCase.indexOf("sqlserver") >= 0 || lowerCase.indexOf("sql server") >= 0) {
                    f481a = "SQLSERVER";
                } else if (lowerCase.indexOf("postgresql") >= 0) {
                    f481a = "POSTGRESQL";
                } else if (lowerCase.indexOf("mariadb") >= 0) {
                    f481a = "MARIADB";
                } else {
                    f482b.error("数据库类型:[" + lowerCase + "]不识别!");
                }
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            } catch (Exception e) {
                f482b.error(e.getMessage(), e);
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            } catch (Throwable th) {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
                throw th;
            }
        }
        return f481a;
    }

    public static String m431a(Connection connection) throws SQLException, DBException {
        if ("".equals(f481a)) {
            String lowerCase = connection.getMetaData().getDatabaseProductName().toLowerCase();
            if (lowerCase.indexOf("mysql") >= 0) {
                f481a = "MYSQL";
            } else if (lowerCase.indexOf("oracle") >= 0) {
                f481a = "ORACLE";
            } else if (lowerCase.indexOf("sqlserver") >= 0 || lowerCase.indexOf("sql server") >= 0) {
                f481a = "SQLSERVER";
            } else if (lowerCase.indexOf("postgresql") >= 0) {
                f481a = "POSTGRESQL";
            } else if (lowerCase.indexOf("mariadb") >= 0) {
                f481a = "MARIADB";
            } else {
                f482b.error("数据库类型:[" + lowerCase + "]不识别!");
            }
        }
        return f481a;
    }

    public static String m430a(String str, String str2) {
        char c = 65535;
        switch (str2.hashCode()) {
            case -1955532418:
                if (str2.equals("ORACLE")) {
                    c = 0;
                    break;
                }
                break;
            case -1620389036:
                if (str2.equals("POSTGRESQL")) {
                    c = 2;
                    break;
                }
                break;
            case 67444:
                if (str2.equals("DB2")) {
                    c = 1;
                    break;
                }
                break;
        }
        switch (c) {
            case 0:
            case 1:
                return str.toUpperCase();
            case 2:
                return str.toLowerCase();
            default:
                return str;
        }
    }

    public static Boolean checkTableIsExist(String tableName) {
        return checkTableIsExist(tableName, (DataBaseConfig) null);
    }

    public static Boolean checkTableIsExist(String tableName, DataBaseConfig var1) {
        Connection var2 = null;
        ResultSet var3 = null;

        Boolean var22;
        try {
            String[] var4 = new String[]{"TABLE"};
            if (var1 == null) {
                var2 = getConnection();
            } else {
                var2 = getConnection(var1);
            }

            DatabaseMetaData var5 = var2.getMetaData();
            DbType var6 = getDbType(var1);
            String var7 = DbTypeUtils.getDbTypeString(var6);
            String var8 = m430a(tableName, var7);
            String var9 = null;
            if (var1 != null) {
                var9 = var1.getUsername();
            } else {
                DataBaseConfig var10 = (DataBaseConfig)SpringContextUtils.getBean(DataBaseConfig.class);
                var9 = var10.getUsername();
            }

            if (DbTypeUtils.dbTypeIsOracle(var6) || DbType.DB2.equals(var6)) {
                var9 = var9 != null ? var9.toUpperCase() : null;
            }

            if (DbTypeUtils.dbTypeIsSQLServer(var6)) {
                var3 = var5.getTables(var2.getCatalog(), (String)null, var8, var4);
            } else if (DbTypeUtils.dbTypeIsPostgre(var6)) {
                var3 = var5.getTables(var2.getCatalog(), "public", var8, var4);
            } else if (DbType.HSQL.equals(var6)) {
                var3 = var5.getTables(var2.getCatalog(), "PUBLIC", var8.toUpperCase(), var4);
            } else {
                var3 = var5.getTables(var2.getCatalog(), var9, var8, var4);
            }

            if (var3.next()) {
                var22 = true;
                return var22;
            }

            var22 = false;
        } catch (SQLException var20) {
            throw new RuntimeException();
        } finally {
            try {
                if (var3 != null) {
                    var3.close();
                }

                if (var2 != null) {
                    var2.close();
                }
            } catch (SQLException var19) {
                log.error(var19.getMessage(), var19);
            }

        }

        return var22;
    }

    public static Map<String, Object> m433a1(List<Map<String, Object>> list) {
        HashMap hashMap = new HashMap();
        for (int i = 0; i < list.size(); i++) {
            hashMap.put(list.get(i).get("column_name").toString(), list.get(i));
        }
        return hashMap;
    }

    public static String getDialect() throws SQLException, DBException {
        return getDbDialect(getDatabaseType());
    }

    public static String getDbDialect(String str) throws SQLException, DBException {
        char c = 65535;
        switch (str.hashCode()) {
            case -1955532418:
                if (str.equals("ORACLE")) {
                    c = 2;
                    break;
                }
                break;
            case -1620389036:
                if (str.equals("POSTGRESQL")) {
                    c = 1;
                    break;
                }
                break;
            case 2185:
                if (str.equals("DM")) {
                    c = 3;
                    break;
                }
                break;
            case 912124529:
                if (str.equals("SQLSERVER")) {
                    c = 0;
                    break;
                }
                break;
        }
        switch (c) {
            case 0:
                return "org.hibernate.dialect.SQLServerDialect";
            case 1:
                return "org.hibernate.dialect.PostgreSQLDialect";
            case 2:
                return "org.hibernate.dialect.OracleDialect";
            case 3:
                return "org.hibernate.dialect.DmDialect";
            default:
                return "org.hibernate.dialect.MySQL5InnoDBDialect";
        }
    }

    public static String m439c(String str) {
        return str;
    }

    public static Connection getConnection(DataBaseConfig bVar) throws SQLException {
        DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
        driverManagerDataSource.setDriverClassName(bVar.getDriverClassName());
        driverManagerDataSource.setUrl(bVar.getUrl());
        driverManagerDataSource.setUsername(bVar.getUsername());
        driverManagerDataSource.setPassword(bVar.getPassword());
        return driverManagerDataSource.getConnection();
    }

    public static DbType getDbType(DataBaseConfig bVar) {
        if (bVar == null) {
            return CommonUtils.getDatabaseTypeEnum();
        }
        return JdbcUtils.getDbType(bVar.getUrl());
    }
}
